var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    bankcardInfoVo: '',
    name: '',
    bankCard: '',
    // bankStatus:"",//
    userData: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //that.onMineInfo();
  },
  //查询用户信息
  onMineInfo: function() {
    var that = this
    wx.request({
      method: 'POST',
      url: app.preUrl + 'findByUserId',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: wx.getStorageSync('userId')
      },
      success: function(res) {
        console.log(res.data.result.auditStatus)
        if (res.data.code == 100) {
          that.setData({
            userData: res.data.result //状态为3，用户认证成功
          })
        }
      },
      fail: function(error) {
        console.log(error)
      }
    })
  },

  //查询银行卡信息
  onBankInfo: function() {
    var that = this
    var userid = wx.getStorageSync('userId')
    console.log(userid)
    wx.request({
      method: 'POST',
      url: app.preUrl + 'bankUserId',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: userid
      },
      success: function(res) {
        console.log(res.data.result)
        if (res.data.code == 100) {
          var name = '*' + res.data.result.bankName.substr(1)
          var bankCard =
            '************' + res.data.result.bankCardNumber.substr(-4)
          that.setData({
            name: name,
            bankCard: bankCard,
            bankcardInfoVo: res.data.result,
            ifShownoData: true
            // auditStatus: 3//3认证成功
          })
          // that.setData({ name: name, bankCard: bankCard, bankcardInfoVo: res.data.result, ifShownoData: true });
        } else if (res.data.code == 400) {
          that.setData({
            ifShownoData: false
            // auditStatus: 3//认证成功
          })
        }
      },
      fail: function(error) {
        console.log(error)
      }
    })
  },
  goBindBandCard: function() {
    var that = this
    console.log(that.data.userData.auditStatus)
    if (that.data.userData.auditStatus != 3) {
      //说明没有认证通过
      wx.showModal({
        title: '提示',
        content: '身份认证成功才能添加银行卡',
        confirmText: '去认证',
        confirmColor: '#56BDE0',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定');
            wx.navigateTo({
              url: 'authentication'
            })
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    } else {
      var that = this
      var userid = wx.getStorageSync('userId')
      wx.request({
        method: 'POST',
        url: app.preUrl + 'bankUserId', //查询我的银行卡
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded;'
        },
        data: {
          userId: userid
        },
        success: function(res) {
          console.log(res.data)
          let relName = that.data.userData.cardName
          let relCard = that.data.userData.cardNum
          let relPhone = that.data.userData.telephone
          // if (res.data.code == 400) {//400说明没有银行
          wx.navigateTo({
            url:
              'bindBandCard?relName=' +
              relName +
              '&relCard=' +
              relCard +
              '&relPhone=' +
              relPhone
          })
        },
        fail: function(error) {
          console.log('error', error)
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.onMineInfo()
    that.onBankInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
